/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Case.java
 *
 * Created on 7 sept. 2011, 20:18:05
 */
package vue;

import java.awt.Color;
import java.util.Observable;
import java.util.Observer;
import model.jeu_de_dame.Case;
import model.util.Couleur;
import model.jeu_de_dame.Dame;
import model.jeu_de_dame.Piece;

/**
 *
 * @author nabil
 */
public class CaseVue extends javax.swing.JPanel implements Observer {

    private int coordonneX; private int coordonneY;
   javax.swing.JLabel piece = null;
    private Color maCouleur; 
    
    public CaseVue(Case c,int x, int y,int taille){
        initComponents();
        this.setLayout(new java.awt.BorderLayout());
        Couleur coul = c.getCouleur();
        this.coordonneX = x;
        this.coordonneY = y;
        if(coul==Couleur.BLANC)
           maCouleur = new Color(255, 255, 255);
        else if(coul==Couleur.NOIR)
           maCouleur = new Color(185, 178, 178);
        this.setBackground(maCouleur);
        if(c.hasPiece())
        {
            this.affecterPion(c.getPiece());
        }
        c.addObserver(this);
    }
    
    private void affecterPion(Piece p)
    {
        PieceVue.TYPE_PION t=null;
        if(p!=null){
            if(p instanceof Dame)
            {
                if(p.getCouleur()==Couleur.BLANC)
                    t=PieceVue.TYPE_PION.DAME_BLANC;
                else
                    t=PieceVue.TYPE_PION.DAME_NOIR;
            }else
            {
                if(p.getCouleur()==Couleur.BLANC)
                    t=PieceVue.TYPE_PION.PION_BLANC;
                else
                    t=PieceVue.TYPE_PION.PION_NOIR;
            }
            this.piece = new PieceVue(p,this.getWidth(), t);
            this.add(piece,java.awt.BorderLayout.CENTER);
            
            if(p.isSelected())
                this.setBackground(Color.CYAN);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vue.DamierApp.class).getContext().getResourceMap(CaseVue.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 361, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        try
        {
            DamierView.getInstance().getControl().selectionCase(this.getCoordonneX(), this.getCoordonneY());
            System.out.println("apres select case");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * @return the x
     */
    public int getCoordonneX() {
        return coordonneX;
    }

    /**
     * @return the y
     */
    public int getCoordonneY() {
        return coordonneY;
    }

    public void update(Observable o, Object arg) {
        try
        {
            if(o instanceof Case)
            {
                this.removeAll();
                this.updateUI();
                Case c = (Case)o;
                
                if(c.isAccessible())
                    this.setBackground(Color.YELLOW);
                else
                    this.setBackground(this.maCouleur);
                
                if(c.hasPiece())
                {
                    this.affecterPion(c.getPiece());
                    
                }
            }
            
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
